##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 3: Foreign Aid Average Treatment Effects

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

##################################################################

rm(list=ls())

##################################################################
# Treatment Effects: Foreign Aid
##################################################################

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")
dep.var <- c("q8","q8.toolittle", "q9")

temp.table <- table(data.working$group, data.working$q8)
# 1 = too much + petition  2 = too much 3 = about the right amount 4 = too little 5 = too little + petition
prop.table(temp.table,1)

ind.group <- c("group3" , "group2")

reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))
matrix <- vector("list", length(dep.var))

x=0 
for(i in 1:length(dep.var)){
	
	my.formula <-paste(dep.var[i],'~', paste(ind.group, collapse= ' + '))
	temp.model <- lm(my.formula, data=data.working)
	temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
	x = x+1
	se.models[[x]] <- temp.se
	reg.models[[x]] <- temp.model

}

varlabels <-       c("Information", "Salience", "Constant (Control Mean)" )
spectitle <- c("Support for Foreign Economic Assistance")
outputfile <- c("tf_t_03_ate_foreign_aid.txt")
depvarcaption <- c("Support for Foreign Economic Assistance")
cutoffs.star <- c(0.1, 0.02)
char.star = c("**", "***")
columnlabels <-c("Ordinal (1-5)", "Increase Budget (1/0)", "Ideal Spending")

data.subset <- data.working[data.working$group2==1 | data.working$group3==1,] 
tresults.1 <- round((t.test(q8~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.2 <- round((t.test(q8.toolittle~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
tresults.3 <- round((t.test(q9~group3, data=data.subset, alternative = c("less"))$p.value), digits=2)
lines.add <- list(c("Information-Salience (p-val.)", tresults.1, tresults.2, tresults.3))

stargazer(reg.models[[1]], reg.models[[2]], reg.models[[3]],
          se=list(se.models[[1]], se.models[[2]], se.models[[3]]),
         type="text",
         title= spectitle,
          out=outputfile,
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = columnlabels,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
         add.lines=lines.add
)   